<?php
class DatabaseManager extends CI_Model {

	public function __construct()
	{
		$this->load->database();
		$this->load->model('RongImManager');
	}
	public function get_user_info($username){
		$this->load->database();
		$query = $this->db->query('SELECT * FROM user where username="'.$username.'" ');
		$row = $query->row_array();
		return $row;
	}
	public function get_user_info_by_phone($phone){
		$this->load->database();
		$query = $this->db->query('SELECT * FROM user where phone="'.$phone.'" ');
		$row = $query->row_array();
		return $row;
	}
	public function get_integral_detail($username){
		$query = $this->db->query('SELECT * FROM integral_detail where username="'.$username.'" order by id desc limit 10');
		$row = $query->result_array();
		return $row;
	}
	public function get_task_by_id($id){
		$this->load->database();
		$query = $this->db->query('SELECT * FROM task where id="'.$id.'"');
		$row = $query->row_array();
		return $row;
	}
	public function get_task_list(){
		$query = $this->db->query('SELECT * FROM task order by id desc');
		$row = $query->result_array();
		for ($i= 0;$i< count($row);$i++){
			$array=$row[$i];
			$id=$array['id'];
			$query = $this->db->query('select user.username,user.name,user.sex,user.avatarUrl from user  inner join task  on user.username=task.username WHERE task.id='.$id);
			$urow = $query->row_array();
			$a=array('user'=>$urow);
			$info=array('id'=>$array['id'],
					'depository'=>$array['depository'],
					'destination'=>$array['destination'],
					'expressCompany'=>$array['expressCompany'],
					'date'=>$array['date'],
					'status'=>$array['status'],);
			$row[$i]=$info+$a;
		}
		return $row;
	}
	public function delete_task($username,$id){
		$this->db->delete('task', array('id' => $id));
		$sql = 'UPDATE user SET releaseCount =releaseCount-1 WHERE username = "'.$username.'"';
		$this->db->query($sql);
	}
	public function get_task_list_by_username($username){
		$query = $this->db->query('SELECT * FROM task where username="'.$username.'" order by id desc limit 20');
		$row = $query->result_array();
		$urow=$this->get_user_info($username);
		$userData=array('username'=>$urow['username'],'sex'=>$urow['sex'],'avatarUrl'=>$urow['avatarUrl']);
		for ($i= 0;$i< count($row);$i++){
			$a=array('user'=>$userData);
			$array=$row[$i];
			$info=array('id'=>$array['id'],
					'depository'=>$array['depository'],
					'destination'=>$array['destination'],
					'expressCompany'=>$array['expressCompany'],
					'date'=>$array['date'],
					'status'=>$array['status'],);
			$row[$i]=$info+$a;
		}
		return $row;
	}
	public function get_task_list_by_receive_user($username){
		$query = $this->db->query('SELECT * FROM task where receiveUser="'.$username.'" order by id desc limit 20');
		$row = $query->result_array();
		for ($i= 0;$i< count($row);$i++){
			$array=$row[$i];
			$urow=$this->get_user_info($array['username']);
			$userData=array('username'=>$urow['username'],'sex'=>$urow['sex'],'avatarUrl'=>$urow['avatarUrl']);
			$a=array('user'=>$userData);
			$info=array('id'=>$array['id'],
					'depository'=>$array['depository'],
					'destination'=>$array['destination'],
					'expressCompany'=>$array['expressCompany'],
					'date'=>$array['date'],
					'status'=>$array['status'],);
			$row[$i]=$info+$a;
		}
		return $row;
	}
	public function save_user($username,$password,$name,$phone,$idcard,$sex){
		$sql = 'insert into user (username,password,name,phone,idcard,sex,integral,createTime) values ("'.$username.'","'.$password.'","'.$name.'","'.$phone.'","'.$idcard.'","'.$sex.'","10","'.time().'")';
		$this->db->query($sql);
	}
	public function save_avatar($username,$name,$url){
		$sql = 'UPDATE user SET avatarUrl = "'.$url.'" WHERE username = "'.$username.'"';
		$this->refresh_rongim_user($username);
		$this->db->query($sql);
	}
	public function sign_in($username,$signInCount,$signInDate,$integral){
		$sql = 'UPDATE user SET signInCount = "'.$signInCount.'",signInDate = "'.$signInDate.'",integral = "'.$integral.'" WHERE username = "'.$username.'"';
		$this->db->query($sql);
	}
	public function add_integral($username,$integral){
		$sql = 'UPDATE user SET integral=integral+'.$integral.' WHERE username = "'.$username.'"';
		$this->db->query($sql);
	}
	public function save_token($username,$token){
		$sql = 'UPDATE user SET token = "'.$token.'" WHERE username = "'.$username.'"';
		$this->db->query($sql);
	}

	private  function refresh_rongim_user($username){
		$data=$this->get_user_info($username);
		$this->RongImManager->refresh_user($data['username'],$data['name'],$data['avatarUrl']);
	}
	public function add_integral_detail($username,$note,$type,$integral,$time){
		$sql = 'insert into integral_detail (username,note,type,count,date) values ("'.$username.'","'.$note.'","'.$type.'","'.$integral.'","'.$time.'")';
		$this->db->query($sql);
	}
	public function release_task($username,$row){
		$sql = 'insert into task (username,expressCompany,courinerNumber,phoneNumber,contactor,heavy,big,depository,depositoryDetail,destination,expressPassword,remark,date,status)
		values ("'.$username.'","'.$row['expressCompany'].'","'.$row['courinerNumber'].'","'.$row['phoneNumber'].'","'.$row['contactor'].'","'.$row['heavy'].'","'.$row['big'].'","'.$row['depository'].'","'.$row['depositoryDetail'].'","'.$row['destination'].'","'.$row['expressPassword'].'","'.$row['remark'].'","'.$row['date'].'","0")';
		$this->db->query($sql);
		$sql = 'UPDATE user SET releaseCount =releaseCount+1 WHERE username = "'.$username.'"';
		$this->db->query($sql);
	}
	public function edit_task($row){
		$this->db->replace('task', $row);
	}
	public function receive_task($username,$id){
		$sql = 'UPDATE task SET status = "1",receiveUser = "'.$username.'",receiveTime="'.time().'" WHERE id = "'.$id.'"';
		$this->db->query($sql);
		$sql = 'UPDATE user SET receiveCount =receiveCount+1 WHERE username = "'.$username.'"';
		return $this->db->query($sql);
	}
	public function finish_task($id){
		$sql = 'UPDATE task SET status = "2",finishTime="'.time().'" WHERE id = "'.$id.'"';
		return $this->db->query($sql);
	}
	public function close_task($id){
		$sql = 'UPDATE task SET status = "3" WHERE id = "'.$id.'"';
		return $this->db->query($sql);
	}
	public function set_friend($username,$friendId){
		$sql = 'insert into friend (userId,friendId,status) values ("'.$username.'","'.$friendId.'","0")';
		$this->db->query($sql);
	}
	public function is_friend($username,$friendId){
		$query = $this->db->query('SELECT * FROM friend where (userId="'.$username.'" and friendId="'.$friendId.'") or (userId="'.$friendId.'" and friendId="'.$username.'")');
		$row = $query->result_array();
		return count($row)!=0;
	}
	public function get_session($username){
		$query = $this->db->query('SELECT * FROM session where username="'.$username.'"');
		$row = $query->row_array();
		return $row;
	}
	public function set_session($username,$sessionId){
		$arr=array(
			'username'=>$username,
			'time'=>time(),
			'sessionId'=>$sessionId
		);
		$this->db->replace('session',$arr);
	}
	public function get_friends_list($username){
		$query = $this->db->query('SELECT * FROM friend where userId="'.$username.'" or friendId="'.$username.'"');
		$row = $query->result_array();
		for ($i= 0;$i< count($row);$i++){
			$data=$row[$i];
			$userId=$data['userId'];
			if($userId==$username){
				$userId=$data['friendId'];
			}
			$user=$this->get_user_info($userId);
			$user=array(
				'username'=>$user['username'],
				'avatarUrl'=>$user['avatarUrl'],
				'name'=>$user['name']
			);
			$list[$i]=$user;
		}
		return $list;
	}
}